package com.atguigu.jue.demo02;


import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @auther zzyy
 * @create 2021-08-05 21:03
 *
 * int
 * Integer
 * AtomicInteger 原子包装类
 */
public class CASDemo {
    public static void main(String[] args) {
//        AtomicInteger atomicInteger=new AtomicInteger(5);
//        System.out.println(atomicInteger.compareAndSet(5,2021)+"\t"+atomicInteger.get());
//        System.out.println(atomicInteger.compareAndSet(5,1024)+"\t"+atomicInteger.get());
        Number number=new Number();
        for (int i = 1; i <=200; i++) {
            new Thread(()->{
                for (int j = 1; j <=200; j++) {
                    number.setNumber();
                    number.setAtomicInteger();
                }
            },String.valueOf(i)).start();
        }
        try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); }
        System.out.println(number.getNumber());
        System.out.println(number.getAtomicInteger());
    }
}
class Number{
    int number=0;

    public int getNumber() {
        return number;
    }

    public synchronized void setNumber() {
        number ++;
    }
    AtomicInteger atomicInteger=new AtomicInteger();

    public int getAtomicInteger() {
        return atomicInteger.get();
    }

    public void setAtomicInteger() {
        atomicInteger.getAndIncrement();
    }
}
